/*
 * @Author: 来自火星的码农 15122322+heyzhi@user.noreply.gitee.com
 * @Date: 2025-09-04 18:58:52
 * @LastEditors: 来自火星的码农 15122322+heyzhi@user.noreply.gitee.com
 * @LastEditTime: 2025-09-19 14:05:22
 * @FilePath: /server/workspace/src/base/fdmanager.h
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#pragma once
#include "base/Singleton.h"
#include "base/mutex.h"
#include <cstdint>
#include <memory>
#include <vector>
namespace  Yizhi{


    class FdCtx:public std::enable_shared_from_this<FdCtx>{

        public:
        typedef std::shared_ptr<FdCtx> ptr;

        FdCtx(int fd);
        ~FdCtx();
        bool init();

        bool isClose(){return  m_isClosed;}
        bool isInint(){return m_isInited;}
        bool isSocket(){return  m_isSocket;}


        bool getUserNonblock(){return  m_userNonblock;}
        void setUserNonblock(bool v){m_userNonblock=v;}

        bool getSysNonblock(){return m_sysNonblock;}
        void setSysNonblock(bool v){m_sysNonblock=v;}

        uint64_t getTime(int type);
        void setTime(uint64_t timeout,int type);

        void close();
        private:
        int m_fd;
        bool m_isInited:1;
        bool m_isClosed:1;
        bool m_isSocket:1;
        bool m_sysNonblock :1;
        bool m_userNonblock:1;



        uint64_t m_recvTimeout;
        uint64_t m_sendTimeout;
    };


    class FdManager{
        public:
        typedef RWMutex RWMutexType;

        FdManager();

        FdCtx::ptr get(int fd,bool auto_create=false);
        void del(int fd);
        
        private:
        RWMutexType m_mutex;
        std::vector<FdCtx::ptr> m_datas;
    };
    typedef  Singleton<FdManager> FdMgr ;

}